package com.xbs.dao.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.ConsumerEntity;

/**
 * 后台用户管理Dao类
 * 1.查询所有用户信息
 * @author Administrator
 *
 */
public class ConsumerDao extends BaseDao {
	
	//声明一个结果处理对象
	private RowDatas<ConsumerEntity> consumer;
	
	/**
	 * 实例结果出来对象
	 */
	public ConsumerDao(){
		consumer = new getConsumer();
	}
	
	/**
	 * 1.根据删除状态查询所有用户信息
	 * @return
	 */
	public List<ConsumerEntity> selAll(int status){
		String sql = "select * from tb_user where user_status =?";
		return queryList(sql, new ParamSet(status), consumer);
	}
	
	/**
	 * 2.根据用户名或电话查询用户信息
	 * @return
	 */
	public List<ConsumerEntity> selAll1(String nametel){
		String sql = "select * from tb_user where user_name = '"+nametel+"' or user_telphone = '"+nametel+"'";
		return queryList(sql, consumer);
	}
	
	/**
	 * 3.添加用户信息
	 * @return
	 */
	public int insert(String name, String idcard, String telphone, String password, String paypassword, String email){
		String sql = "insert into tb_user(user_name, user_idcard, user_telphone, user_password, user_paypassword, user_email) "
				+ "values(?, ?, ?, ?, ?, ?)";
		return updateExecuted(sql, new ParamSet(name, idcard, telphone, password, paypassword, email));
	}
	
	/**
	 * 4.逻辑删除用户信息
	 * @return
	 */
	public int delete(String idcard){
		String sql = "update tb_user set user_status = 0 where user_idcard = ?";
		return updateExecuted(sql, new ParamSet(idcard));
	}
	
	/**
	 * 5.根据身份证号码查询用户信息
	 * @return
	 */
	public List<ConsumerEntity> selAll2(String idcard){
		String sql = "select * from tb_user where user_idcard = ?";
		return queryList(sql, new ParamSet(idcard), consumer);
	}
	
	/**
	 * 6.根据身份证号码修改用户信息
	 * @return
	 */
	public int update(String name, String idcard, String telphone, String password, String paypassword, String email){
		String sql = "update tb_user set user_name = ?, user_telphone = ?, user_password = ?, user_email = ?, user_paypassword =? where user_idcard = ? ";
		return updateExecuted(sql, new ParamSet(name, telphone, password, email, paypassword, idcard));
	}
	
	private class getConsumer implements RowDatas<ConsumerEntity>{
		@Override
		public ConsumerEntity getRowData(ResultSet rs) throws SQLException {
			ConsumerEntity consumer = new ConsumerEntity();
			consumer.setUser_name(rs.getString("user_name"));
			consumer.setUser_idcard(rs.getString("user_idcard"));
			consumer.setUser_telphone(rs.getString("user_telphone"));
			consumer.setUser_password(rs.getString("user_password"));
			consumer.setUser_paypassword(rs.getString("user_paypassword"));
			consumer.setUser_email(rs.getString("user_email"));
			consumer.setUser_status(rs.getInt("user_status"));
			return consumer;
		}
	}
}
